<template>
  <scroll-view enable-back-to-top scroll-y class="scroll-view" @scrolltolower="onScrolltolower">
    <!-- 已登录: 显示购物车 -->
    <template v-if="memberStore.profile">
      <!-- 购物车列表 -->
      <view class="cart-list" v-if="showCartList">
        <view class="cart-list" v-for="row in cartList" :key="row.tenantId">
          <!-- 优惠提示 -->
          <view class="tips">
            <text class="label"><uni-icons type="shop" color="#27ba9b" size="18"></uni-icons></text>
            <text class="desc">{{ row?.shopName }}</text>
          </view>
          <!-- 滑动操作分区 -->
          <uni-swipe-action>
            <!-- 滑动操作项 -->
            <uni-swipe-action-item
              v-for="(item, index) in row.records"
              :key="index"
              class="cart-swipe"
            >
              <!-- 商品信息 -->
              <view class="goods">
                <!-- 选中状态 -->
                <text
                  @tap="onChangeSelected(item)"
                  class="checkbox"
                  :class="{ checked: item.selected }"
                ></text>
                <navigator
                  :url="`/pages/goods/goods?id=${item.productId}`"
                  hover-class="none"
                  class="navigator"
                >
                  <image mode="aspectFill" class="picture" :src="item.imageUrl"></image>
                  <view class="meta">
                    <view class="name ellipsis">{{ item.productName }}</view>
                    <view class="attrsText ellipsis">{{ item.sku }}</view>
                    <view class="price">{{ item.nowPrice }}</view>
                  </view>
                </navigator>
                <!-- 商品数量 -->
                <view class="count">
                  <uni-number-box v-model="item.quantity" :min="1" @change="onChangeCount(item)" />
                </view>
              </view>
              <!-- 右侧删除按钮 -->
              <template #right>
                <view class="cart-swipe-right">
                  <button @tap="onDeleteCart(item.cartId)" class="button delete-button">
                    删除
                  </button>
                </view>
              </template>
            </uni-swipe-action-item>
          </uni-swipe-action>
        </view>
      </view>
      <!-- 购物车空状态 -->
      <view class="cart-blank" v-else>
        <image src="/static/images/blank_cart.png" class="image" />
        <text class="text">购物车还是空的，快来挑选好货吧</text>
        <view hover-class="none">
          <button @tap="onHome" class="button">去首页看看</button>
        </view>
      </view>
      <!-- 吸底工具栏 -->
      <view
        v-if="showCartList"
        class="toolbar"
        :style="{ paddingBottom: safeAreaInsetBottom ? safeAreaInsets?.bottom + 'px' : 0 }"
      >
        <text @tap="onChangeSelectedAll" class="all" :class="{ checked: isSelectedAll }">全选</text>
        <text class="text">合计:</text>
        <text class="amount">{{ selectedCartListMoney }}</text>
        <view class="button-grounp">
          <view
            @tap="gotoPayment"
            class="button payment-button"
            :class="{ disabled: selectedCartListCount === 0 }"
          >
            去结算({{ selectedCartListCount }})
          </view>
        </view>
      </view>
    </template>
    <!-- 未登录: 提示登录 -->
    <view class="login-blank" v-else>
      <text class="text">登录后可查看购物车中的商品</text>
      <navigator url="/pages/login/login" hover-class="none">
        <button class="button">去登录</button>
      </navigator>
    </view>
    <!-- 猜你喜欢 -->
    <GuessLike ref="guessRef" />
    <!-- 底部占位空盒子 -->
    <view class="toolbar-height"></view>
  </scroll-view>
</template>

<script setup lang="ts">
import {
  cartPriceByIdAPI,
  deleteMemberCartAPI,
  getMemberCartAPI,
  putMemberCartBySkuIdAPI,
} from '@/services/cart'
import { useMemberStore } from '@/stores'
import { onShow } from '@dcloudio/uni-app'
import { ref } from 'vue'
import GuessLike from '@/components/GuessLike.vue'

const guessRef = ref(GuessLike)

// 是否适配底部安全区域
defineProps<{
  safeAreaInsetBottom?: boolean
}>()

// 获取屏幕边界到安全区域距离
const { safeAreaInsets } = uni.getSystemInfoSync()

// 获取会员Store
const memberStore = useMemberStore()

// 获取购物车数据
const cartList = ref<any[]>([])
const selectedCartIdList = ref<any[]>([])
const selectedCartListMoney = ref<any>(0)
const selectedCartListCount = ref<any>(0)
const isSelectedAll = ref(false)
// 优化购物车空列表状态，默认展示列表
const showCartList = ref(true)
const getMemberCartData = async () => {
  const res = await getMemberCartAPI({})
  cartList.value = res.data
  showCartList.value = res.data.length > 0
}

// 初始化调用: 页面显示触发
onShow(() => {
  if (memberStore.profile) {
    getMemberCartData()
  }
})

// 点击删除按钮
const onDeleteCart = (cartId: string) => {
  // 弹窗二次确认
  uni.showModal({
    content: '是否删除',
    confirmColor: '#27BA9B',
    success: async (res) => {
      if (res.confirm) {
        // 后端删除单品
        await deleteMemberCartAPI([cartId])
        // 重新获取列表
        getMemberCartData()
      }
    },
  })
}

// 修改商品数量
const onChangeCount = (row: any) => {
  putMemberCartBySkuIdAPI(row)
}

// 修改选中状态-单品修改
const onChangeSelected = async (item: any) => {
  // 前端数据更新-是否选中取反
  item.selected = !item.selected
  calculatePrice()
}

// 购物车价格
const calculatePrice = async () => {
  let ids = []
  for (let i in cartList.value) {
    for (let j in cartList.value[i].records) {
      if (cartList.value[i].records[j].selected) {
        ids.push(cartList.value[i].records[j].cartId)
      }
    }
  }
  selectedCartIdList.value = ids
  if (selectedCartIdList.value.length > 0) {
    let res = await cartPriceByIdAPI(selectedCartIdList.value)
    selectedCartListMoney.value = res.data.price
  } else {
    selectedCartListMoney.value = 0
  }
  selectedCartListCount.value = selectedCartIdList.value.length
}

// 修改选中状态-全选修改
const onChangeSelectedAll = () => {
  // 全选状态取反
  isSelectedAll.value = !isSelectedAll.value
  // 前端数据更新
  cartList.value.forEach((item) => {
    item.records.forEach((value) => {
      value.selected = isSelectedAll.value
    })
  })
  // 后端数据更新
  calculatePrice()
}

// 结算按钮
const gotoPayment = async () => {
  if (selectedCartListCount.value === 0) {
    return uni.showToast({
      icon: 'none',
      title: '请选择商品',
    })
  }

  // 跳转到结算页
  uni.navigateTo({ url: '/pagesOrder/create/create?cartIds=' + selectedCartIdList.value })
}
const onHome = async () => {
  uni.switchTab({ url: '/pages/index/index' })
}

const onScrolltolower = async () => {
  guessRef.value!.getMore()
}
</script>

<style scoped lang="scss">
// 根元素
:host {
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background-color: #f7f7f8;
}

// 滚动容器
.scroll-view {
  flex: 1;
  background-color: #f7f7f8;
}

// 购物车列表
.cart-list {
  padding: 0 20rpx;

  // 优惠提示
  .tips {
    display: flex;
    align-items: center;
    line-height: 1;
    margin: 30rpx 10rpx;
    font-size: 26rpx;
    color: #666;

    .label {
      color: #27ba9b;
      padding: 7rpx 15rpx 5rpx;
      border-radius: 4rpx;
      font-size: 24rpx;
    }
  }

  // 购物车商品
  .goods {
    display: flex;
    padding: 20rpx 20rpx 20rpx 80rpx;
    border-radius: 10rpx;
    background-color: #fff;
    position: relative;

    .navigator {
      display: flex;
    }

    .checkbox {
      position: absolute;
      top: 0;
      left: 0;

      display: flex;
      align-items: center;
      justify-content: center;
      width: 80rpx;
      height: 100%;

      &::before {
        content: '\e6cd';
        font-family: 'erabbit' !important;
        font-size: 40rpx;
        color: #444;
      }

      &.checked::before {
        content: '\e6cc';
        color: #27ba9b;
      }
    }

    .picture {
      width: 170rpx;
      height: 170rpx;
    }

    .meta {
      flex: 1;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      margin-left: 20rpx;
    }

    .name {
      height: 72rpx;
      font-size: 26rpx;
      color: #444;
    }

    .attrsText {
      line-height: 1.8;
      padding: 0 15rpx;
      font-size: 24rpx;
      align-self: flex-start;
      border-radius: 4rpx;
      color: #888;
      background-color: #f7f7f8;
    }

    .price {
      line-height: 1;
      font-size: 26rpx;
      color: #444;
      margin-bottom: 2rpx;
      color: #cf4444;

      &::before {
        content: '￥';
        font-size: 80%;
      }
    }

    // 商品数量
    .count {
      position: absolute;
      bottom: 20rpx;
      right: 5rpx;

      display: flex;
      justify-content: space-between;
      align-items: center;
      width: 220rpx;
      height: 48rpx;

      .text {
        height: 100%;
        padding: 0 20rpx;
        font-size: 32rpx;
        color: #444;
      }

      .input {
        height: 100%;
        text-align: center;
        border-radius: 4rpx;
        font-size: 24rpx;
        color: #444;
        background-color: #f6f6f6;
      }
    }
  }

  .cart-swipe {
    display: block;
    margin: 20rpx 0;
  }

  .cart-swipe-right {
    display: flex;
    height: 100%;

    .button {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 50px;
      padding: 6px;
      line-height: 1.5;
      color: #fff;
      font-size: 26rpx;
      border-radius: 0;
    }

    .delete-button {
      background-color: #cf4444;
    }
  }
}

// 空状态
.cart-blank,
.login-blank {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  height: 60vh;
  .image {
    width: 400rpx;
    height: 281rpx;
  }
  .text {
    color: #444;
    font-size: 26rpx;
    margin: 20rpx 0;
  }
  .button {
    width: 240rpx !important;
    height: 60rpx;
    line-height: 60rpx;
    margin-top: 20rpx;
    font-size: 26rpx;
    border-radius: 60rpx;
    color: #fff;
    background-color: #27ba9b;
  }
}

// 吸底工具栏
.toolbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: calc(var(--window-bottom));
  z-index: 1;

  height: 100rpx;
  padding: 0 20rpx;
  display: flex;
  align-items: center;
  border-top: 1rpx solid #ededed;
  border-bottom: 1rpx solid #ededed;
  background-color: #fff;
  box-sizing: content-box;

  .all {
    margin-left: 25rpx;
    font-size: 14px;
    color: #444;
    display: flex;
    align-items: center;
  }

  .all::before {
    font-family: 'erabbit' !important;
    content: '\e6cd';
    font-size: 40rpx;
    margin-right: 8rpx;
  }

  .checked::before {
    content: '\e6cc';
    color: #27ba9b;
  }

  .text {
    margin-right: 8rpx;
    margin-left: 32rpx;
    color: #444;
    font-size: 14px;
  }

  .amount {
    font-size: 20px;
    color: #cf4444;

    .decimal {
      font-size: 12px;
    }

    &::before {
      content: '￥';
      font-size: 12px;
    }
  }

  .button-grounp {
    margin-left: auto;
    display: flex;
    justify-content: space-between;
    text-align: center;
    line-height: 72rpx;
    font-size: 13px;
    color: #fff;

    .button {
      width: 240rpx;
      margin: 0 10rpx;
      border-radius: 72rpx;
    }

    .payment-button {
      background-color: #27ba9b;

      &.disabled {
        opacity: 0.6;
      }
    }
  }
}
// 底部占位空盒子
.toolbar-height {
  height: 100rpx;
}
</style>
